package com.changfang.gold_man.controller;

import com.changfang.gold_man.common.ImageUtil;
import com.changfang.gold_man.common.ResCode;
import com.changfang.gold_man.common.ResultVO;

import com.changfang.gold_man.mapper.SwiperMapper;
import com.changfang.gold_man.service.Imp.SwiperServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;


@RestController
@RequestMapping("/swiper")
@CrossOrigin(value = "*",maxAge = 1800,allowedHeaders = "*")
public class SwiperController {
    @Autowired
    private SwiperMapper swiperMapper;
    @Autowired
    private SwiperServiceImpl swiperServiceImpl;
    //新增轮播图记录
    @PostMapping("/upload")
    public ResultVO upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
//        如果文件不为空
        if(!file.isEmpty()){
            try {
                ImageUtil imageUtil = new ImageUtil();
                String maker = request.getParameter("maker");
                System.out.println(maker);
                System.out.println(file);
                String url = ImageUtil.fileUpload(file);
                swiperMapper.upload(maker,"0",url);
                return new ResultVO(ResCode.SUCCESS, "上传成功");
            }catch (Exception e){
                return new ResultVO(ResCode.ERROR, "上传失败");
            }
        }
        return new ResultVO(ResCode.ERROR, "上传失败");
    }

    //查询所有广告图
    @GetMapping("/findAll")
    public ResultVO findAll(){
        try {
            return new ResultVO(ResCode.SUCCESS, swiperServiceImpl.findAll());
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR, "查询失败");
        }
    }
    //查询所有动态壁纸
    @GetMapping("/wallpaper")
    public ResultVO wallpaper(){
        try {
            return new ResultVO(ResCode.SUCCESS, swiperServiceImpl.wallPaper());
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR, "查询失败");
        }
    }
    //删除
    @GetMapping("/delete")
    public ResultVO delete(@RequestParam String imgId){
        try {
            swiperServiceImpl.delete(imgId);
            return new ResultVO(ResCode.SUCCESS, "删除成功");
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR, "删除失败");
        }
    }

    //修改轮播图权限
    @PostMapping("/updateStatus")
    public ResultVO updateStatus(@RequestBody Map<String,String> swiperInfo){
        String imgId = swiperInfo.get("imgId");
        String status = swiperInfo.get("status");
        try {
            swiperServiceImpl.updateStatus(imgId,status);
            return new ResultVO(ResCode.SUCCESS, "修改权限成功");
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR, "修改权限失败");
        }
    }

    //查找所有可用轮播图
    @GetMapping("/findEnable")
    public ResultVO findEnable(){
        try {
            return new ResultVO(ResCode.SUCCESS, swiperServiceImpl.findEnable());
        }catch (Exception e){
            return new ResultVO(ResCode.ERROR, "查询轮播图失败");
        }
    }

}
